﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using Canwest.Broadcasting.Windows.Classes.CW_Common_2008;
using System.IO;

namespace CW_Format_Guidelines
{
    public partial class Form1 : Form
    {

        private DataSet m_resultSet = null;
        private DataTable m_resultTable = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void m_startButton_Click(object sender, EventArgs e)
        {
            String dqlStr = "SELECT object_name, r_object_id, i_folder_id ";
            dqlStr += "FROM dm_document ";
            dqlStr += "WHERE i_cabinet_id = '0c01b1df8000250d' ";
            dqlStr += "and a_content_type like '%excel%' and i_folder_id IS NOT NULL ";
            dqlStr += "ENABLE(ROW_BASED)";

            using (CWDctmWS.DocumentumDataAccess dctmWS = new CW_Format_Guidelines.CWDctmWS.DocumentumDataAccess())
            {
                dctmWS.Credentials = CredentialCache.DefaultNetworkCredentials;

                try
                {
                    m_resultSet = dctmWS.QuerySession_fromDocbase(dqlStr, "DMS");
                }
                catch (Exception ex)
                {
                    ErrorForm errMsg = new ErrorForm(ex, null);
                    errMsg.Show();
                }

                m_resultTable = m_resultSet.Tables[0];

                m_resultTable.Columns.Add("Folder_Path");

                foreach (DataRow row in m_resultTable.Rows)
                {
                    String folderId = row.Field<String>(2);
                    dqlStr = "SELECT r_folder_path FROM dm_folder WHERE r_object_id = '" + folderId + "' ENABLE(ROW_BASED)";

                    DataSet tempSet = dctmWS.QuerySession_fromDocbase(dqlStr, "DMS");
                    DataTable tempTable = tempSet.Tables[0];
                    String folderPaths = null;

                    foreach (DataRow dr in tempTable.Rows)
                    {
                        String currFolder = dr[0].ToString();
                        if (!String.IsNullOrEmpty(currFolder))
                        {
                            folderPaths += currFolder + "|";
                        }
                    }

                    row.SetField<String>(3, folderPaths);

                }

            }

            m_dataGrid.DataSource = m_resultTable;


        }

        private void m_SaveButton_Click(object sender, EventArgs e)
        {
            FileStream excelSheet = new FileStream(@"D:\Bogus\formatguidelines.xls", FileMode.CreateNew);
            m_dataGrid.ExportToXls(excelSheet);
        }
    }
}
